package com.leedsoft.controller;

import com.leedsoft.common.AjaxReply;
import com.leedsoft.common.JSON;
import com.leedsoft.common.Page;
import com.leedsoft.controller.dto.OrgMemDto;
import com.leedsoft.controller.vo.NewOrgMemVo;
import com.leedsoft.controller.vo.OrgMemPageVo;
import com.leedsoft.domain.AidOrg;
import com.leedsoft.domain.SysPmiUser;
import com.leedsoft.service.OrgMemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 救援机构成员管理
 */
@Controller
@RequestMapping("/org_mem")
public class OrgMemController {

    @Autowired
    private OrgMemService orgMemService;

    @RequestMapping("/viewOrgMem")
    public ModelAndView viewOrgMem(ModelAndView modelAndView, HttpServletRequest request){
        String orgId = request.getParameter("id");
        List<SysPmiUser> sysUser_list = orgMemService.querySysUser();//获取系统用户信息
        List<AidOrg> aidOrg_list = orgMemService.queryAidOrg();//获取救援机构信
        modelAndView.addObject("sysUser_Json", JSON.gson.toJson(sysUser_list));
        modelAndView.addObject("sysUser",sysUser_list);
        modelAndView.addObject("aidOrg",aidOrg_list);
        modelAndView.addObject("orgId",orgId);
        modelAndView.setViewName("orgMember/orgMember-list");
        return  modelAndView;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(OrgMemPageVo memPageVo,HttpServletRequest request){
        String org_id = request.getParameter("orgId");
        if (org_id!=null&&!"".equals(org_id)){
            memPageVo.setOrg_id(Long.parseLong(org_id));
        }else{
            memPageVo.setOrg_id(0);
        }
        Page<OrgMemDto> page = orgMemService.getOrgMemPage(memPageVo);
        Map<String,Object> map = new HashMap();
        map.put("total",page.getCount());
        map.put("rows",page.getList());
        return map;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(NewOrgMemVo newOrgMemVo){
        if (newOrgMemVo.getMem_id()==null){
            orgMemService.addOrgMem(newOrgMemVo);
            String ok = "ok";
            return AjaxReply.success(ok,001);
        }else {
            orgMemService.updateOrgMem(newOrgMemVo);
            String ok = "ok";
            return AjaxReply.success(ok,001);
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(HttpServletRequest request){
        String om_id = request.getParameter("om_id");
        String mem_id = request.getParameter("mem_id");
        orgMemService.deleteOrgMem(Long.parseLong(om_id),Long.parseLong(mem_id));
        String ok = "ok";
        return AjaxReply.success(ok,001);
    }
}
